//
//  CustomOrderTableViewCell.m
//  ChunLaiZhuangClient
//
//  Created by zzs on 16/3/3.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "CustomOrderTableViewCell.h"
#import "MyOrderFormViewController.h"
#import "OrderDetailViewController.h"
#import "CLZWebviewViewController.h"
@implementation CustomOrderTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *identifier = @"CustomOrderTableViewCell";
    // 1.缓存中取
    CustomOrderTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    // 2.创建
    if (cell == nil) {
        cell = [[CustomOrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    return cell;
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor=[UIColor colorWithHexString:@"efeff4"];
        _bgView=[[UIView alloc] initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,100)];
        _bgView.backgroundColor=[UIColor whiteColor];
        _timeLabel=[[UILabel alloc] initWithFrame:CGRectMake(19, 8, 140, 30)];
        _timeLabel.font=[UIFont systemFontOfSize:14];
        _timeLabel.textAlignment=NSTextAlignmentLeft;
        _timeLabel.textColor=[UIColor colorWithHexString:@"000000"];
        _timeLabel.text=@"2016.01.01  10：23：52";
        [_bgView addSubview:_timeLabel];
        _payStatus=[[UILabel alloc] initWithFrame:CGRectMake(19, 8, SCREEN_WIDTH-140-30, 30)];
        _payStatus.right=SCREEN_WIDTH-17;
        _payStatus.font=[UIFont systemFontOfSize:15];
        _payStatus.textAlignment=NSTextAlignmentRight;
        _payStatus.textColor=[UIColor colorWithHexString:@"f35959"];
        _payStatus.text=@"待支付";
        [_bgView addSubview:_payStatus];
        UIView *lineView=[[UIView alloc] initWithFrame:CGRectMake(15, 40, SCREEN_WIDTH-15, 0.5)];
        lineView.backgroundColor=[UIColor colorWithHexString:@"f1f1f1"];
        [_bgView addSubview:lineView];
        _lineView1=[[UIView alloc] initWithFrame:CGRectMake(15, 40, SCREEN_WIDTH-15, 0.5)];
        _lineView1.backgroundColor=[UIColor colorWithHexString:@"f1f1f1"];
        [_bgView addSubview:_lineView1];
        _bgScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0,_lineView1.bottom, SCREEN_WIDTH, 60)];
        [_bgView addSubview:_bgScrollView];
        //商品个数
        _countLabel=[[UILabel alloc] initWithFrame:CGRectMake(19,_lineView1.bottom+10, 150, 25)];
        _countLabel.font=[UIFont systemFontOfSize:12];
        _countLabel.textAlignment=NSTextAlignmentLeft;
        _countLabel.textColor=[UIColor colorWithHexString:@"000000"];
        [_bgView addSubview:_countLabel];
        
        //总金额
        _amountLabel=[[UILabel alloc] initWithFrame:CGRectMake(19, _lineView1.bottom+10, 200, 25)];
        _amountLabel.right=SCREEN_WIDTH-17;
        _amountLabel.font=[UIFont boldSystemFontOfSize:12];
        _amountLabel.textAlignment=NSTextAlignmentRight;
        _amountLabel.textColor=[UIColor colorWithHexString:@"f35959"];
        [_bgView addSubview:_amountLabel];
        
        //去支付
        _payBt=[UIButton buttonWithType:UIButtonTypeCustom];
        _payBt.frame=CGRectMake(0, _amountLabel.bottom+10,176/2, 28);
        _payBt.right=SCREEN_WIDTH-17;
        _payBt.backgroundColor=[UIColor colorWithHexString:@"f35959"];
        _payBt.layer.cornerRadius=15;
        _payBt.clipsToBounds=YES;
        [_payBt addTarget:self action:@selector(payAction:) forControlEvents:UIControlEventTouchUpInside];
        [_payBt setTitle:@"去支付" forState:UIControlStateNormal];
        _payBt.titleLabel.font=[UIFont systemFontOfSize:14];
        [_payBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_bgView addSubview:_payBt];
        
        //取消订单
        _cancleBt=[UIButton buttonWithType:UIButtonTypeCustom];
        _cancleBt.frame=CGRectMake(0, _amountLabel.bottom+10,176/2, 28);
        _cancleBt.right=_payBt.left-10;
        _cancleBt.layer.cornerRadius=15;
        _cancleBt.clipsToBounds=YES;
        _cancleBt.layer.borderColor=[UIColor colorWithHexString:@"bebebe"].CGColor;
        _cancleBt.layer.borderWidth=1;
        [_cancleBt addTarget:self action:@selector(cancleAction:) forControlEvents:UIControlEventTouchUpInside];
         _cancleBt.titleLabel.font=[UIFont systemFontOfSize:14];
        [_cancleBt setTitle:@"取消订单" forState:UIControlStateNormal];
        [_cancleBt setTitleColor:[UIColor colorWithHexString:@"bebebe"] forState:UIControlStateNormal];
        [_bgView addSubview:_cancleBt];
        [self addSubview:_bgView];
        
        //确认收货
        _receiveBt=[UIButton buttonWithType:UIButtonTypeCustom];
        _receiveBt.frame=CGRectMake(0, _amountLabel.bottom+15,176/2, 28);
        _receiveBt.right=SCREEN_WIDTH-17;
        _receiveBt.layer.cornerRadius=15;
        _receiveBt.clipsToBounds=YES;
        _receiveBt.layer.borderColor=[UIColor colorWithHexString:@"bebebe"].CGColor;
        _receiveBt.layer.borderWidth=1;
        [_receiveBt addTarget:self action:@selector(comfirmAction:) forControlEvents:UIControlEventTouchUpInside];
        _receiveBt.titleLabel.font=[UIFont systemFontOfSize:14];
        [_receiveBt setTitle:@"确认收货" forState:UIControlStateNormal];
        [_receiveBt setTitleColor:[UIColor colorWithHexString:@"bebebe"] forState:UIControlStateNormal];
        [_bgView addSubview:_receiveBt];

        _lineView2=[[UIView alloc] initWithFrame:CGRectMake(15,_receiveBt.bottom+15, SCREEN_WIDTH-15, 0.5)];
        _lineView2.backgroundColor=[UIColor colorWithHexString:@"f1f1f1"];
        _lineView2.hidden=YES;
        [_bgView addSubview:_lineView2];
        
        _beeBt=[UIButton buttonWithType:UIButtonTypeCustom];
        _beeBt.frame=CGRectMake(0, _cancleBt.bottom+15, SCREEN_WIDTH, 40);
        [_beeBt setTitle:@"小蜜蜂实地采购过程" forState:UIControlStateNormal];
        _beeBt.titleLabel.font=[UIFont systemFontOfSize:15];
        _beeBt.titleEdgeInsets=UIEdgeInsetsMake(0, -150, 0, 0);
        UIImageView *playImageView=[[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-50, 5, 30, 30)];
        playImageView.image=[UIImage imageNamed:@"btn_play"];
        [_beeBt addSubview:playImageView];
        [_beeBt addTarget:self action:@selector(toBeeAction:) forControlEvents:UIControlEventTouchUpInside];
        [_beeBt setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _beeBt.titleLabel.textAlignment=NSTextAlignmentLeft;
        [_bgView addSubview:_beeBt];
        _beeBt.hidden=YES;
        
    }
    return self;
}


-(void)setFrameModel:(OrderCellFrameModel *)frameModel{
    
    if (!self.buttonsArray) {
        self.buttonsArray=[[NSMutableArray alloc] init];
    }else{
        if (self.buttonsArray.count>0) {
            for(UIButton *bt in self.buttonsArray){
                [bt removeFromSuperview];
            }
        }
    }

    _frameModel=frameModel;
    
    float edge=19.0;
    float widh=(SCREEN_WIDTH-38-30)/6;
    float distance=6;
    for (int i=0; i<frameModel.dataModel.pictureArray.count; i++) {
        _bgScrollView.contentSize=CGSizeMake(edge*2+widh*frameModel.dataModel.pictureArray.count+distance*(frameModel.dataModel.pictureArray.count-1), 60);
        AccountOrdersProductListModel *productModel=frameModel.dataModel.pictureArray[i];
        UIButton *imageBt=[UIButton buttonWithType:UIButtonTypeCustom];
        imageBt.frame=CGRectMake(0, 0, widh,widh);
        [imageBt sd_setImageWithURL:[NSURL URLWithString:productModel.image] forState:UIControlStateNormal placeholderImage:nil];
        imageBt.left=edge+(widh+distance)*i;
        imageBt.top=5;
        imageBt.tag=i;
        [_bgScrollView addSubview:imageBt];
        imageBt.layer.borderColor=[UIColor colorWithHexString:@"f1f1f1"].CGColor;
        imageBt.clipsToBounds=YES;
        imageBt.layer.borderWidth=0.5;
        [self.buttonsArray addObject:imageBt];
    }
    self.timeLabel.text=frameModel.dataModel.date_added;
    self.countLabel.text=[NSString stringWithFormat:@"共%@件商品",frameModel.dataModel.product_total];
    self.payStatus.text=frameModel.dataModel.status;
    _amountLabel.text=[NSString stringWithFormat:@"总金额: %@",frameModel.dataModel.total];
    // 2、您的订单已经打包完成 3、包裹已经发出 7、已取消  5、已完成  1、订单已经开始配货    15、收到订单
    if ([frameModel.dataModel.status_id intValue]==0){
        _cancleBt.hidden=NO;
        _payBt.hidden=NO;
        _receiveBt.hidden=YES;
        if (frameModel.dataModel.bee.length>0) {
            _beeBt.hidden=NO;
            _bgView.height=190-5+40;
             _lineView2.hidden=NO;
        }else{
            _beeBt.hidden=YES;
            _bgView.height=190-5;
             _lineView2.hidden=YES;
        }
    }
    if ([frameModel.dataModel.status_id intValue]==3){
        _cancleBt.hidden=YES;
        _payBt.hidden=YES;
        _receiveBt.hidden=NO;
       
        if (frameModel.dataModel.bee.length>0) {
            _beeBt.hidden=NO;
             _bgView.height=190-5+40;
             _lineView2.hidden=NO;
        }else{

            _beeBt.hidden=YES;
             _lineView2.hidden=YES;
            _bgView.height=190-5;
        }

    }

    
    _lineView1.bottom=40+frameModel.imageHight;
    _amountLabel.top=_lineView1.bottom+10;
    _countLabel.top=_lineView1.bottom+10;
    _cancleBt.top=_amountLabel.bottom+8;
    _payBt.top=_amountLabel.bottom+8;
    _receiveBt.top=_amountLabel.bottom+15;
    _beeBt.top=_cancleBt.bottom+15;
    _lineView2.top=_cancleBt.bottom+15;
    _bgScrollView.top=40;
}
#pragma mark-小蜜蜂
-(void)toBeeAction:(UIButton*)bt{
    CLZWebviewViewController *webVC=[[CLZWebviewViewController alloc] init];
    webVC.webTitle=@"小蜜蜂采购过程";
    webVC.webUrl=_frameModel.dataModel.bee;
     MyOrderFormViewController *orderVC=(MyOrderFormViewController*)[CustomOrderTableViewCell viewController:self];
    [orderVC.navigationController pushViewController:webVC animated:YES];
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark-取消订单
-(void)cancleAction:(UIButton*)bt{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"确定取消订单吗？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag=10;
    [alert show];
}
#pragma mark-去支付
-(void)payAction:(UIButton*)bt{
     MyOrderFormViewController *orderVC=(MyOrderFormViewController*)[CustomOrderTableViewCell viewController:self];
    OrderDetailViewController *detailVC=[[OrderDetailViewController alloc] init];
    detailVC.orderID=_frameModel.dataModel.order_id;
    [orderVC.navigationController pushViewController:detailVC animated:YES];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (alertView.tag) {
        case 10:
        {
            if (buttonIndex==1) {
                
                [ApiClient cancelOrder_ID:_frameModel.dataModel.order_id didFinishLoaded:^(KXJson *json, NSString *path) {
                    [self performSelector:@selector(reloadOrder) withObject:nil afterDelay:0];
                    [[LKDHud shareHud] showtip:[json getStringForKey:@"message"]];
                    
                } didFailLoaded:^(NSError *error, NSString *path) {
                    
                }];
            }
  
        }
            break;
        case 11:
        {
            if (buttonIndex==1) {
                
               [ApiClient comfirmOrder_finishOrdierID:_frameModel.dataModel.order_id didFinishLoaded:^(KXJson *json, NSString *path) {
                   
                   [self performSelector:@selector(reloadOrder) withObject:nil afterDelay:0];
                   [[LKDHud shareHud] showtip:[json getStringForKey:@"message"]];
               } didFailLoaded:^(NSError *error, NSString *path) {
                   
               }];
        }
  
        }
            break;
   
        default:
            break;
    }
    }
-(void)reloadOrder{
    MyOrderFormViewController *orderVC=(MyOrderFormViewController*)[CustomOrderTableViewCell viewController:self];
    [orderVC loadSelectIndex:orderVC.selectIndex];

}
#pragma mark-确认收货
-(void)comfirmAction:(UIButton*)bt{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"确定收到货吗？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag=11;
    [alert show];
}
@end
